package com.zxy.supplier_system.server.entity

import jakarta.persistence.*

/**
 * 商品组合
 */
@Entity
@Table(
    uniqueConstraints = [
        UniqueConstraint(
            name = "composite_sub_goods_key_unique_constraint",
            columnNames = ["sub_goods_id", "source_goods"]
        ),
        UniqueConstraint(
            name = "composite_goods_key_unique_constraint",
            columnNames = ["goods_id", "source_goods"]
        ),
    ]
)
class CompositeGoods {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    var goods: Goods? = null

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    var subGoods: SubGoods? = null

    /**
     * 需要的数量
     */
    var amount: Int = 0

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn
    var sourceGoods: Goods? = null

    @get:Transient
    val inStore:Int
        get() {
            return subGoods?.amount?:goods?.amount?:0
        }

}